import { useRequest } from 'ahooks'
import { getQuestionListService } from '../service/question'
import { useSearchParams } from 'react-router-dom'
import {
  LIST_SEARCH_PARAMS_KEyWORDS,
  LIST_PAGE_PARAMS_KEY,
  LIST_PAGE_SIZE_PARAMS_KEY,
  LIST_PAGE_SIZE,
} from '../constant/index'
type OptionType = {
  isStar: boolean
  isDeleted: boolean
}
function useLoadQuestionList(opt: Partial<OptionType> = {}) {
  //   const { isStar = false, isDeleted = false } = opt
  const [searchParams] = useSearchParams()
  const { isStar, isDeleted } = opt

  async function load() {
    const keyword = searchParams.get(LIST_SEARCH_PARAMS_KEyWORDS) || ''
    const page = (searchParams.get(LIST_PAGE_PARAMS_KEY) || 1) as number
    const pageSize = (searchParams.get(LIST_PAGE_SIZE_PARAMS_KEY) || LIST_PAGE_SIZE) as number

    const data = await getQuestionListService({ keyword, page, pageSize, isStar, isDeleted })
    return data
  }
  const { loading, data, error, refresh } = useRequest(load, {
    refreshDeps: [searchParams],
  })
  return { loading, data, error, refresh }
}
export default useLoadQuestionList
